package advancedMobs.entity.AI;

import advancedMobs.entity.advancedMob.AdvancedMob;
import advancedMobs.entity.zombie.EntityAdvancedZombie;
import net.minecraft.entity.EntityCreature;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.ai.EntityAIBase;
import net.minecraft.entity.ai.RandomPositionGenerator;
import net.minecraft.util.Vec3;

public class EntityAIMoveTowardsLeader extends EntityAIBase
{
    private AdvancedMob theEntity;
    private AdvancedMob targetEntity;
    private double movePosX;
    private double movePosY;
    private double movePosZ;
    private float field_75425_f;
    private float field_75426_g;
    private double distanceFromLeader = 75;

    public EntityAIMoveTowardsLeader(AdvancedMob par1EntityCreature, float par2, float par3)
    {
        this.theEntity = par1EntityCreature;
        this.field_75425_f = par2; //movement speed?
        this.field_75426_g = par3; //maximum distance?
        this.setMutexBits(1);
    }

    /**
     * Returns whether the EntityAIBase should begin execution.
     */
    public boolean shouldExecute()
    {
    	this.targetEntity = this.theEntity.returnLeader();
    	//no leader
        if (this.targetEntity == null)
        {
        	//System.out.println("No Leader");
            return false;
        }
        //
        else if (this.targetEntity.getDistanceSqToEntity(this.theEntity) > (double)(this.field_75426_g * this.field_75426_g))
        {
        	//System.out.println("Distance to far");
            return false;
        }
        else if(this.targetEntity.getDistanceSqToEntity(this.theEntity) < distanceFromLeader)
        {
        	//System.out.println("Reached Leader");
        	return false;
        }
        else
        {
            Vec3 vec3 = RandomPositionGenerator.findRandomTargetBlockTowards(this.theEntity, 16, 7, this.theEntity.worldObj.getWorldVec3Pool().getVecFromPool(this.targetEntity.posX, this.targetEntity.posY, this.targetEntity.posZ));

            if (vec3 == null)
            {
            	//System.out.println("Couldn't find path");
                return false;
            }
            else
            {
                this.movePosX = vec3.xCoord;
                this.movePosY = vec3.yCoord;
                this.movePosZ = vec3.zCoord;
                return true;
            }
        }
    }

    /**
     * Returns whether an in-progress EntityAIBase should continue executing
     */
    public boolean continueExecuting()
    {
    	//System.out.println("Still Executing");
        Boolean test = !this.theEntity.getNavigator().noPath() && this.targetEntity.isEntityAlive() && this.targetEntity.getDistanceSqToEntity(this.theEntity) > distanceFromLeader;
        //System.out.println(test + " distance " + this.targetEntity.getDistanceSqToEntity(this.theEntity));
        return test;
    }

    /**
     * Resets the task
     */
    public void resetTask()
    {
    	//System.out.println("reset");
        this.targetEntity = null;
    }

    /**
     * Execute a one shot task or start executing a continuous task
     */
    public void startExecuting()
    {
    	//System.out.println("Trying to move");
        this.theEntity.getNavigator().tryMoveToXYZ(this.movePosX, this.movePosY, this.movePosZ, this.field_75425_f);
    }
}
